<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  
  <link rel="shortcut icon" href="img/favicon.ico">
  <title>Lambda - Java Note</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="css/theme.css" />
  <link rel="stylesheet" href="css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "Lambda";
    var mkdocs_page_input_path = "Lambda.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="js/jquery-2.1.1.min.js" defer></script>
  <script src="js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
        <a href="." class="icon icon-home"> Java Note</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="./search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="index.html">Home</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="StringX.html">StringX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Collection.html">Collection</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="IO.html">IOFile</a>
                    </li>
                </ul>
                <ul class="current">
                    <li class="toctree-l1 current"><a class="reference internal current" href="Lambda.html">Lambda</a>
    <ul class="current">
    <li class="toctree-l2"><a class="reference internal" href="#fomulation">Fomulation</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#type">Type</a>
        <ul>
    <li class="toctree-l3"><a class="reference internal" href="#_1">上下文</a>
    </li>
        </ul>
    </li>
    </ul>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Generic.html">Generic</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Stream.html">Stream</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Atomic.html">Atomic</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Reflect.html">Reflect</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="JavaFX.html">JavaFX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Sundry.html">Sundry</a>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href=".">Java Note</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href=".">Docs</a> &raquo;</li>
    
      
    
    <li>Lambda</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h2 id="lambda">Lambda</h2>
<h3 id="fomulation">Fomulation</h3>
<pre><code class="language-java">(parameters) -&gt; expression
// or
(parameters) -&gt; {statements;}
</code></pre>
<ul>
<li><strong>可选类型声明：</strong>不需要声明参数类型，编译器可以统一识别参数值</li>
<li><strong>可选的参数圆括号：</strong>一个参数无需定义圆括号，但多个参数需要定义圆括号</li>
<li><strong>可选的大括号：</strong>如果主体包含了一个语句，就不需要使用大括号</li>
<li><strong>可选的返回关键字：</strong>如果主体只有一个表达式返回值则编译器会自动返回值，大括号需要指定明表达式返回了一个数值</li>
</ul>
<blockquote>
<p>类型</p>
<p>主要可以做成接口类型 或者 Runnable</p>
</blockquote>
<p>注意：</p>
<ol>
<li><strong>lambda 表达式只能引用标记了 final 的外层局部变量</strong> ( 不确定,OpenJDK8下 非final可以)</li>
<li>如果选择省略参数类型，必须省略所有参数的类型</li>
<li>Lambda 表达式当中不允许声明一个与局部变量同名的参数或者局部变量</li>
<li><strong>block statements</strong> 使用 return 给出返回值</li>
</ol>
<h3 id="type">Type</h3>
<blockquote>
<p>lambda表达式的类型为<strong>函数式接口</strong></p>
<p>Lambda 表达式本身不能用作独立表达式</p>
<p>Lambda 表达式的类型由编译器推断</p>
</blockquote>
<p>函数式接口</p>
<ol>
<li>具有一个方法的接口</li>
<li>不能为<ol>
<li>默认方法 <code>default</code></li>
<li>静态方法 <code>static</code></li>
<li>从 Object 类继承的方法</li>
</ol>
</li>
</ol>
<pre><code class="language-java">public class Lambda{
    public static void main(String[] args){
        opera oi=(a,b)-&gt;a+b;//忽略类型，编译器计算
        //opera oi=(int a,int b)-&gt;a+b;// the same object
        System.out.println(oi.op(1,3));
        final String v=&quot;hello&quot;;
        greet g=msg-&gt; System.out.println(msg);//greet g=msg-&gt; System.out.println(msg+v); 亦可使用
        /**
        * @brief 值的等价形式
        *  (String msg) -&gt;sout(msg);
        *  (msg) -&gt; sout(msg);
        *  msg -&gt; {sout(msg);}
        */
        g.op(v);
    }
    static public int operation(int a,int b,opera op){// as a parameter
        return op.op(a,b);
    }
}
@FunctionalInterface //标记一个函数式接口
interface Operation&lt;E&gt;{
    E op(E a,E b);
}
interface Opera{
    int op(int a,int b);
}
interface greet{
    void op(String info);
}
</code></pre>
<h4 id="_1">上下文</h4>
<ol>
<li>可以在return语句中使用lambda表达式，其目标类型在方法返回类型中声明</li>
</ol>
<pre><code class="language-java">public class Main {
  public static void main(String[] argv) {
    System.out.println(create().calculate(2, 2));//the core code
  }
  private static Calculator create(){
    return (x,y)-&gt; x / y;
  }  
}

@FunctionalInterface
interface Calculator{
  long calculate(long x, long y);
}
</code></pre>
<ol>
<li>可以使用一个lambda表达式前面加一个cast。在转换中指定的类型是其目标类型</li>
</ol>
<pre><code class="language-java">public class Main {
  public static void main(String[] argv) {
    engine((IntCalculator) ((x,y)-&gt; x + y));
  }
  private static void engine(IntCalculator calculator){
    int x = 2, y = 4;
    int result = calculator.calculate(x,y);
    System.out.println(result);
  }
  private static void engine(LongCalculator calculator){
    long x = 2, y = 4;
    long result = calculator.calculate(x,y);
    System.out.println(result);
  }  
}

@FunctionalInterface
interface IntCalculator{
  int calculate(int x, int y);
}

@FunctionalInterface
interface LongCalculator{
  long calculate(long x, long y);
}
</code></pre>
<p><strong><a href="https://www.w3cschool.cn/java/java-intersection-type.html">More</a></strong></p>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="Generic.html" class="btn btn-neutral float-right" title="Generic">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="IO.html" class="btn btn-neutral" title="IOFile"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="IO.html" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="Generic.html" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '.';</script>
    <script src="js/theme.js" defer></script>
      <script src="search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
